from django.apps import AppConfig
from .dingtalk_client import DingtalkCient
import threading
from asgiref.sync import async_to_sync
import sys


def set_interval(func, sec):
    def func_wrapper():
        func()
        set_interval(func, sec)
    t = threading.Timer(sec, func_wrapper)
    t.start()
    return t


timer = ""


class DingtalkServiceConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'dingtalk_service'

    def ready(self) -> None:
        if 'runserver' in sys.argv:
            set_interval(DingtalkCient.refresh_access_token, 3600)
            DingtalkCient.refresh_access_token()
